package com.imcode;

import java.sql.*;
import java.util.Scanner;

public class AppOperation {

    public static void Create(int id,String name,int age,Double height) throws SQLException {

        Connection con = DBUtil.getConnection();
        String sql = "INSERT INTO t_student(id,name,age,height) VALUES(?,?,?,?)";
        PreparedStatement st = con.prepareStatement(sql);
        st.setInt(1,id);
        st.setString(2,name);
        st.setInt(3, age);
        st.setDouble(4,height);
        Integer count = st.executeUpdate(); // 返回的是操作的数据的条数
        System.out.println("影响的条数：" + count);
        System.out.println("增加成功！");
        st.close();
        con.close();
        System.out.println("输入1回到主页面，输入2退出该系统");

    }

    public static void Delete(int id) throws SQLException {

        Connection con = DBUtil.getConnection();
        String sql = "DELETE FROM t_student where id = ?";
        PreparedStatement st = con.prepareStatement(sql);
        st.setInt(1,id);
        Integer count = st.executeUpdate(); // 返回的是操作的数据的条数
        System.out.println("影响的条数：" + count);
        System.out.println("增加成功！");
        st.close();
        con.close();
        System.out.println("输入1回到主页面，输入2退出该系统");
    }

    public static void Update(int upid,int id,String name,int age,Double height) throws SQLException {

        Connection con = DBUtil.getConnection();
        String sql = "UPDATE t_student set id = ?,name = ?,age = ?,height = ? where id = ?";
        PreparedStatement st = con.prepareStatement(sql);
        st.setInt(1,id);
        st.setString(2,name);
        st.setInt(3, age);
        st.setDouble(4,height);
        st.setInt(5,upid);
        Integer count = st.executeUpdate(); // 返回的是操作的数据的条数
        System.out.println("影响的条数：" + count);
        System.out.println("增加成功！");
        st.close();
        con.close();
        System.out.println("输入1回到主页面，输入2退出该系统");
    }

    public static void Select(int id) throws SQLException {

        Connection con = DBUtil.getConnection();
        String sql = "Select * FROM t_student where id = ?";
        PreparedStatement st = con.prepareStatement(sql);
        st.setInt(1,id);
        ResultSet rs = st.executeQuery();
        while(rs.next()){
            System.out.println("id: "+rs.getInt("id")+"\tname: "+rs.getString("name")+"\tage: "+rs.getInt("age")+"\theight: "+rs.getDouble("height"));
        }
        DBUtil.close(con,st,rs);
        System.out.println("输入1回到主页面，输入2退出该系统");
    }

    public static void Interface(){
        System.out.println("增加班级人员请输入1！");
        System.out.println("删除班级人员请输入2！");
        System.out.println("修改人员信息请输入3！");
        System.out.println("查询人员信息请输入4！");
        System.out.println("退出则输入任意其他值");
    }

    public static void main(String[] args) throws SQLException {
        Interface();
        Scanner in = new Scanner(System.in);
        while(in.hasNext()){
            int n = in.nextInt();
            if(n==1){
                System.out.println("请输入新增人员的id,名字，年龄，身高:");
                int id = in.nextInt();
                String name = in.next();
                int age = in.nextInt();
                Double height = in.nextDouble();
                Create(id,name,age,height);
                int k = in.nextInt();
                if(k==1){
                    Interface();
                }else{
                    break;
                }
            }
            else if(n==2){
                System.out.println("请输入要删除人物的id:");
                int id = in.nextInt();
                Delete(id);
                int k = in.nextInt();
                if(k==1){
                    Interface();
                }else{
                    break;
                }
            }
            else if(n==3){
                System.out.println("请先输入要更新人员的id，再输入要更新人员的id,名字，年龄，身高:");
                int upid = in.nextInt();
                int id = in.nextInt();
                String name = in.next();
                int age = in.nextInt();
                Double height = in.nextDouble();
                Update(upid,id,name,age,height);
                int k = in.nextInt();
                if(k==1){
                    Interface();
                }else{
                    break;
                }
            }
            else if(n==4){
                System.out.println("请输入要查询人员的id:");
                int id = in.nextInt();
                Select(id);
                int k = in.nextInt();
                if(k==1){
                    Interface();
                }else{
                    break;
                }
            }
            else{
                break;
            }
        }
    }
}
